/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/22/14
 * Time: 9:13 AM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.utility.asyncStateMachine
{
import com.societyGames.system.EventObject;

import flash.events.Event;
import flash.events.EventDispatcher;

/**
 * A state machine that handles states that have an exit that may be asynchronous.
 */
public class StateMachine extends EventDispatcher
{
  private var _stateChangedEventObject:EventObject;
  private var _state:State;

  public function StateMachine()
  {
    _stateChangedEventObject = new EventObject(this, "STATE_CHANGED", Event);
  }

  public function gotoState(state:State):void
  {
    if (_state != null) //if we don't have a current state, act like a fake one just finished exiting.
    {
      _state.exit();
    }
    _state = state;
    state.enter();
    _stateChangedEventObject.dispatchEvent();
  }

  public function get state():State
  {
    return _state;
  }

  public function get stateChangedEventObject():EventObject
  {
    return _stateChangedEventObject;
  }
}
}
